﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuoteOfTheDay
{
    public delegate void NewQuotePublishedHandler(object sender, EventArgs e);

    /// <summary>
    /// Make a “Quote of the Day” assembly and GUI
    /// -CurrentQuote property
    /// -QuotePostedOn DateTime property
    /// -RefreshQuote method
    /// -AddQuote method
    /// -RemoveQuote method
    /// -NewQuote event
    /// The Quote class is a quote that can be displayed in a GUI. I may
    /// have completely missed the concept of this class. I don't know if
    /// I understood what the RefreshQuote, AddQuote, and RemoveQuote methods
    /// were intended to do.
    /// </summary>
    public class Quote
    {
        private static List<Quote> quotes;
        private int currLoc;

        private string message;
        public string Message
        {
            get { return message; }
            set { message = value; }
        }
        /// <summary>
        /// The CurrentQuote property houses the current quote
        /// string.
        /// </summary>
        private Quote currentQuote;
        public Quote CurrentQuote
        {
            get { return currentQuote; }
        }

        /// <summary>
        /// The QuotePostedOn can be used to set the date that the
        /// quote was posted.
        /// </summary>
        private DateTime quotePostedOn;
        public DateTime QuotePostedOn
        {
            get { return quotePostedOn; }
            set { quotePostedOn = value; }
        }

        public Quote()
        {
            Message = "A man who dares to waste one hour of time has not discovered the value of life. ~ Charles Darwin";
            quotePostedOn = DateTime.Now;
            currentQuote = this;
            quotes = new List<Quote>();
            quotes.Add(this);
            currLoc = 0;
        }

        public Quote(string msg)
        {
            Message = msg;
            quotePostedOn = DateTime.Now;
            currentQuote = this;
        }

        public event NewQuotePublishedHandler QuotePublished;

        /// <summary>
        /// RefreshQuote fires the event QuotePublished if there
        /// are any listeners. Right now its set up if the quote box
        /// gets emptied it will refresh the quote that is supposed to
        /// be there. I'm not sure really what to use this method for?
        /// </summary>
        public void RefreshQuote()
        {
            if (currLoc == 0)
            {
                currLoc = quotes.Count - 1;
                currentQuote = quotes[currLoc];
            }
            else
            {
                currentQuote = quotes[--currLoc];
            }

            if (QuotePublished != null)
            {
                QuotePublished(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Changes the currentQuote message since the property is
        /// read-only. Then fires the QuotePublished event if there
        /// are listeners attached.
        /// </summary>
        /// <param name="msg"></param>
        public void AddQuote(Quote q)
        {
            currentQuote = q;
            quotes.Add(q);
            currLoc = quotes.Count - 1;
            if (QuotePublished != null)
            {
                QuotePublished(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Remove quote just replaces the CurrentQuote message
        /// with "Quote Removed..." and then refreshes through the
        /// refresh method of the class.
        /// </summary>
        public void RemoveQuote(Quote q)
        {
            quotes.Remove(q);
            currLoc = quotes.Count;
            RefreshQuote();
        }
    }
}
